package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Author cisimu
 * @Date 2025/10/22 15:36
 */
@Service
public class WechatServiceImpl implements WechatService {

    @Value("${wechat.appId}")
    private String appId;
    @Value("${wechat.secret}")
    private String secret;

    private static final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    private static final String ACCESSTOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
    private static final String PHONENUM_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    private Map<String,Object> getCommonParam(){
        Map<String, Object> param = new HashMap<>();
        param.put("appid",appId);
        param.put("secret",secret);
        return param;
    }

    /**
     * 根据临时凭证码获取openId
     *
     * @param code
     */
    @Override
    public String getOpenId(String code) {
        Map<String, Object> param = this.getCommonParam();
        param.put("js_code",code);
        param.put("grant_type","authorization_code");
        String responseBody = HttpUtil.get(OPENID_URL, param);
        if (StrUtil.isBlank(responseBody)) {
            throw new BaseException("获取openid失败");
        }
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        String openId = jsonObject.getStr("openid");
        return openId;
    }

    /**
     * 获取小程序的访问token
     * @return
     */
    private String getAccessToken(){
        Map<String,Object> param = this.getCommonParam();
        param.put("grant_type","client_credential");
        String result = HttpUtil.get(ACCESSTOKEN_URL, param);
        if (StrUtil.isBlank(result)) {
            throw new BaseException("获取小程序访问token失败");
        }
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(result);
        String accessToken = jsonObject.getStr("access_token");
        return accessToken;
    }

    /**
     * 根据手机号获取凭证码获取手机号
     *
     * @param code
     */
    @Override
    public String getPhoneNum(String code) {
        String requestBody = JSONUtil.toJsonStr(Map.of("code", code));
        String responseBody = HttpUtil.post(PHONENUM_URL+this.getAccessToken(),requestBody);
        if (StrUtil.isBlank(responseBody)) {
            throw new BaseException("获取phoneNum失败");
        }
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        String phoneInfoJSON = jsonObject.getStr("phone_info");
        if (StrUtil.isBlank(phoneInfoJSON)) {
            throw new BaseException("获取phoneNum失败");
        }
        JSONObject phoneInfoJsonObject = JSONUtil.parseObj(phoneInfoJSON);
        String phoneNumber = phoneInfoJsonObject.getStr("phoneNumber");
        return phoneNumber;
    }
}
